package com.hugh.glrpcprovider.config;

import com.hugh.service.UserService;
import common.netty.server.NettyServerStart;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: Hugh
 * @Date: 2020/6/13
 */
@Component
@Slf4j
public class ServiceBeanConfig implements ApplicationListener<ContextRefreshedEvent> {

    private Map<String, Object> processMapping = new ConcurrentHashMap<String, Object>();

    @SneakyThrows
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        if(Objects.nonNull(applicationContext)){
            Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(Service.class);
            processMapping = beansWithAnnotation;
            new NettyServerStart().startWithPort(8001, beansWithAnnotation);
        }
    }
}
